<?php
namespace Tongcheng\Controller;
use Think\Controller;
class QuanController extends CommonController {
    public function index(){
    	if(IS_AJAX){
            $model = M("Quan");

            $map['tp_quan.wid'] = session('wid');
            $res['count'] = $model->where($map)->count();
            $page = $_GET['page']?$_GET['page']:1;
            $limit = $_GET['limit']?$_GET['limit']:10;
            $res['list'] = $model->join('tp_member ON tp_quan.openid = tp_member.openid')->where($map)->field("tp_quan.*, tp_member.nickname, tp_member.headimgurl")->order("last_time desc")->page("{$page},{$limit}")->select();
            $this->ajaxReturn($res);
        }else{
            $this->display();
        }
    }
    //圈子
    public function circle(){
        if(IS_AJAX){
            $model = M("circle");
            $map['tp_circle.wid'] = session('wid');
            $map['tp_circle.status'] = 0;
            $res['count'] = $model->where($map)->count();
            $page = $_GET['page']?$_GET['page']:1;
            $limit = $_GET['limit']?$_GET['limit']:10;
            $res['list'] = $model->join('tp_member ON tp_circle.openid = tp_member.openid')->where($map)->field("tp_circle.*, tp_member.nickname, tp_member.headimgurl")->order("last_time desc")->page("{$page},{$limit}")->select();
            foreach($res['list'] as $k=>$v){
                $r = M('tui')->where(['type'=>'quan','aid'=>$v['id']])->find();
                if(!$r){
                    $res['list'][$k]['is_tui'] = 0;
                }else{
                    $res['list'][$k]['is_tui'] = $r['status'];
                }
            }
            $this->ajaxReturn($res);
        }else{
            $this->display();
        }
    }
    // 获取热门话题列表
    public function topicList(){
        $map['wid'] = session('wid');
        $map['topic'] = array('neq', '');
        $list = M("Quan")->where($map)->group('topic')->order("last_time desc")->select();
        foreach ($list as $key => $value) {
            $list[$key]['pics'] = explode(',', $value['pics']);
            $tui = M("tui")->where(['content'=>$value['topic'],'wid'=>session('wid')])->find();
            if(!$tui || !$tui['status']){
                $list[$key]['is_tui'] = 0;
            }else{
                $list[$key]['is_tui'] = 1;
            }
        }
        $this->assign('adv',$list);
        $this->display();
    }
    //轮播图
    public function banner ()
    {
        $model = M("Adv");
        $map['wid'] = session('wid');
        $map['type'] = 'quan';
        $list = $model->where($map)->select();
        $this->assign('list',$list);

        $adv = array("quan"=>"同城圈轮播");
        $this->assign('adv',$adv);
        $this->display();
    }
    //添加轮播图
    public function addbanner(){
        if(IS_AJAX){
            $model = M("Adv");
            $new['wid'] = session('wid');
            $new['type'] = I('post.type');
            $new['imgurl'] = I('post.imgurl');
            $new['create_time'] = date("Y-m-d H:i:s");
            if($model->add($new)){
                $this->success('添加成功！');
            }else{
                $this->error('网络忙，请稍后再试！');
            }
        }else{
            $this->display();
        }
    }
    // 礼物价格管理
    public function gift(){
        $model = M("QuanGift");
        $map['wid'] = session('wid');
        if(IS_POST){
            foreach ($_POST['list'] as $k => $v) {
                if(!is_numeric($k) || !is_numeric($v)){
                    $this->error('网络忙，请稍后再试！');
                }
            }
            foreach ($_POST['list'] as $k => $v) {
                $map['id'] = $k;
                $model->where($map)->setField('price',$v);
            }
            $this->success('修改成功！');
        }else{
            $this->gift_first();
            $list = $model->where($map)->select();
            $this->assign('list',$list);
            $this->display();
        }
    }
    // 礼物价格初始化
    public function gift_first(){
        $model = M("QuanGift");
        $map['wid'] = session('wid');
        if(!$model->where($map)->find()){
            $map['wid'] = 0;
            $list = $model->where($map)->select();
            foreach ($list as $k => $v) {
                unset($list[$k]['last_time']);
                unset($list[$k]['id']);
                $list[$k]['wid'] = session('wid');
            }
            $model->addAll($list);
        }
    }

    public function top(){
        $model = M("Quan");
        $map['wid'] = session('wid');
        $map['id'] = $_POST['id'];

        $new['is_top'] = $_POST['status'];
        if($model->where($map)->save($new)){
            $this->success("置顶成功！");
        }else{
            $this->error("网络忙，请稍后再试！".$model->getLastSql());
        }
    }

    public function tui(){
        $model = M("Quan");
        $type = I("type");
        //推荐话题
        if($type=='topic'){
            $r = tui("topic",0,session('wid'),$_POST['status'],I('content'));
        }
        //推荐用户(同城圈管理)
        elseif($type=='member'){
            $quan = M('quan')->where(['id'=>I('id')])->find();
            if($quan){
                $member = M('member')->where(['openid'=>$quan['openid']])->find();
                if(!$member){
                    $this->error("数据库没有此用户");
                }else{
                    $a = M('quan')->where(['openid'=>$member['openid'],'wid'=>session('wid')])->save(['is_tui'=>$_POST['status'],'last_time'=>$quan['last_time']]);
                    $r = tui('member',$member['mid'],session('wid'),$_POST['status']);
                }
            }else{
                $this->error("网络忙，请稍后再试！");
            }
        }
        //推荐圈子
        elseif($type=='quan'){
            $r = tui("quan",$_POST['id'],session('wid'),$_POST['status']);
        }
        else{
            $map['wid'] = session('wid');
            $map['id'] = $_POST['id'];
            $new['is_tui'] = $_POST['status'];
            $r = $model->where($map)->save($new);
        }
        if($r){
            $this->success("推荐成功！");
        }else{
            $this->error("网络忙，请稍后再试！");
        }
    }
    //删除圈子
    public function delc(){
        $model = M("circle");
        $map['wid'] = session('wid');
        $map['id'] = $_POST['id'];

        if($model->where($map)->delete()){
            $this->success("删除成功！");
        }else{
            $this->error("网络忙，请稍后再试！");
        }
    }

    public function del(){
        $model = M("Quan");
        $map['wid'] = session('wid');
        $map['id'] = $_POST['id'];

        if($model->where($map)->delete()){
            $this->success("删除成功！");
        }else{
            $this->error("网络忙，请稍后再试！");
        }
    }
}